<template>
  <div>
  </div>
</template>

<script>
import { Loading } from 'element-ui'
export default {
  name: 'Loading',
  data () {
    return {
    }
  },
  created: function () {
    this.getToken()
  },
  methods: {
    getToken () {
      let postData = this.$qs.stringify({
        grant_type: 'client_credentials',
        client_id: config.blogService.ClientId,
        client_secret: config.blogService.ClientSecret
      })
      let loadingInstance = Loading.service({
        fullscreen: true,
        text: '加载中'
      })
      this.$axios({
        url: config.blogService.tokenHostUrl + 'connect/token',
        method: 'post',
        data: postData
      }).then((res) => {
        if (res.status === 200) {
          this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
            loadingInstance.close()
          })
          console.log('请求了token')
          this.$store.commit('set_token', res.data.token_type + ' ' + res.data.access_token)
          // 登陆成功后, 跳转到用户未登陆前要访问的页面
          this.$router.push({ path: this.$route.query.redirect })
        }
      }).catch((err) => {
        console.log(err)
        this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
          loadingInstance.close()
        })
      })
    }
  }
}
</script>
<style scoped>

</style>
